/*
 * Copyright [vega-ai-sdk] [rightbrain.art]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * According to vega feature, we modify some class，comment, field name, etc.
 */

package com.rightbrain.vegaai.tool.sdk.service;

import com.alibaba.fastjson.JSONObject;
import com.rightbrain.vegaai.tool.sdk.common.*;
import com.rightbrain.vegaai.tool.sdk.config.VegaConfig;
import com.rightbrain.vegaai.tool.sdk.exception.VegaAIException;
import com.rightbrain.vegaai.tool.sdk.utils.VegaBeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Slf4j
@Component
public class VegaService {

    @Autowired
    private VegaHttpService vegaHttpService;

    @Autowired
    private VegaConfig vegaConfig;

    /**
     * Get ticket by openId.
     * If error return "".
     *
     * @return String.
     */
    public String getTicket() {
        String path = "?openId=" + vegaConfig.getOpenId();
        try {
            Resource resource = vegaHttpService.doGet(Constants.GET_TICKET, path);
            if (resource != null && ResourceCode.OK.code().equals(resource.getCode())) {
                log.info("get ticket success for openId {}, the ticket is {}", vegaConfig.getOpenId(), resource.getData().toString());
                return resource.getData().toString();
            } else if (resource != null) {
                log.error("fail to get ticket , {}", resource.getMessage());
            }
        } catch (IOException | VegaAIException e) {
            log.error("fail to get ticket , error info is {}", e.getMessage());
        } catch (Exception ex) {
            log.error("fail to get ticket , unknown error {}", ex.getMessage());
        }
        return "";
    }

    /**
     * Get Lora Code By Name.
     * If error return Resource.ERROR.
     * Check ResourceCode.OK.code().equals(resource.getCode()).
     *
     * @return Resource.
     */
    public Resource getLoraCodeByName(String loraName) {
        String path = StringUtils.isBlank(loraName) ? "" : "?loraName=" + loraName;
        try {
            return vegaHttpService.doGet(Constants.GET_LORA_MODELS, path);
        } catch (IOException | VegaAIException e) {
            log.error("fail to get lora , error info is {}", e.getMessage());
        } catch (Exception ex) {
            log.error("fail to get lora , unknown error {}", ex.getMessage());
        }
        return Resource.ERROR("请求错误");
    }

    /**
     * Get image for text 2 image.
     * If error return Resource.ERROR.
     * Check ResourceCode.OK.code().equals(resource.getCode()).
     *
     * @return Resource.
     */
    public Resource getImage4Text(String requestid) {
        if (StringUtils.isEmpty(requestid)) {
            return Resource.ERROR("requestid 不能为空");
        }
        String path = "?requestid=" + requestid;
        try {
            return vegaHttpService.doGet(Constants.GET_TEXT_2_IMAGE, path);
        } catch (IOException | VegaAIException e) {
            log.error("fail to get lora , error info is {}", e.getMessage());
        } catch (Exception ex) {
            log.error("fail to get lora , unknown error {}", ex.getMessage());
        }
        return Resource.ERROR("请求错误");
    }

    /**
     * Get image for image 2 image.
     * If error return Resource.ERROR.
     * Check ResourceCode.OK.code().equals(resource.getCode()).
     *
     * @return Resource.
     */
    public Resource getImage4Image(String requestid) {
        if (StringUtils.isEmpty(requestid)) {
            return Resource.ERROR("requestid 不能为空");
        }
        String path = "?requestid=" + requestid;
        try {
            return vegaHttpService.doGet(Constants.GET_IMAGE_2_IMAGE, path);
        } catch (IOException | VegaAIException e) {
            log.error("fail to get lora , error info is {}", e.getMessage());
        } catch (Exception ex) {
            log.error("fail to get lora , unknown error {}", ex.getMessage());
        }
        return Resource.ERROR("请求错误");
    }

    /**
     * Create image by text.
     * If error return Resource.ERROR.
     * Check ResourceCode.OK.code().equals(resource.getCode()).
     *
     * @return Resource.
     */
    public Resource text2Image(Text2ImagesVO text2ImagesVO) {
        if (text2ImagesVO == null) {
            return Resource.ERROR("参数 不能为空");
        }
        try {
            return vegaHttpService.doPostWithJson(Constants.TEXT_2_IMAGE, JSONObject.toJSONString(text2ImagesVO));
        } catch (IOException | VegaAIException e) {
            log.error("fail to get lora , error info is {}", e.getMessage());
        } catch (Exception ex) {
            log.error("fail to get lora , unknown error {}", ex.getMessage());
        }
        return Resource.ERROR("请求错误");
    }

    /**
     * Create image by image.
     * If error return Resource.ERROR.
     * Check ResourceCode.OK.code().equals(resource.getCode()).
     *
     * @return Resource.
     */
    public Resource image2Image(Image2ImageVO image2ImageVO) {
        if (image2ImageVO == null) {
            return Resource.ERROR("参数 不能为空");
        }
        try {
            return vegaHttpService.doPostWithJson(Constants.IMAGE_2_IMAGE, JSONObject.toJSONString(image2ImageVO));
        } catch (IOException | VegaAIException e) {
            log.error("fail to get lora , error info is {}", e.getMessage());
        } catch (Exception ex) {
            log.error("fail to get lora , unknown error {}", ex.getMessage());
        }
        return Resource.ERROR("请求错误");
    }
}
